package plp.functional2;

import plp.expressions2.expression.Expressao;
import plp.expressions2.expression.Valor;
import plp.expressions2.memory.AmbienteCompilacao;
import plp.expressions2.memory.AmbienteExecucao;
import plp.expressions2.memory.ContextoCompilacao;
import plp.expressions2.memory.ContextoExecucao;
import plp.expressions2.memory.ListaValor;
import plp.expressions2.memory.VariavelJaDeclaradaException;
import plp.expressions2.memory.VariavelNaoDeclaradaException;

public class Programa {

	private Expressao exp;
	
	public Programa(Expressao exp) {
		this.exp = exp;
	}

	public ListaValor executar() throws VariavelJaDeclaradaException, VariavelNaoDeclaradaException {
		AmbienteExecucao ambiente = new ContextoExecucao();
		Valor avaliar = exp.avaliar(ambiente);
		ListaValor listaValor = ambiente.getSaida();
		listaValor.write(avaliar);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listaValor;
	}

	public boolean checaTipo() throws VariavelJaDeclaradaException, VariavelNaoDeclaradaException {
		AmbienteCompilacao ambComp = new ContextoCompilacao();
		return exp.checaTipo(ambComp);
	}

	public Expressao getExpressao() {
		return exp;
	}

}
